/**
 * React Query (TanStack Query) Configuration
 *
 * 配置全局Query Client,包括:
 * - 默认查询选项
 * - 默认mutation选项
 * - 缓存策略
 * - 错误处理
 */

import { QueryClient } from '@tanstack/react-query'

/**
 * 创建Query Client实例
 */
export const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      // 重试策略
      retry: 1, // 失败后重试1次
      retryDelay: attemptIndex => Math.min(1000 * 2 ** attemptIndex, 30000), // 指数退避

      // 缓存策略
      staleTime: 5 * 60 * 1000, // 5分钟内数据视为新鲜
      gcTime: 10 * 60 * 1000, // 10分钟后清理缓存 (原cacheTime)

      // 窗口焦点
      refetchOnWindowFocus: false, // 窗口获得焦点时不自动重新获取
      refetchOnMount: true, // 组件挂载时重新获取

      // 网络状态
      refetchOnReconnect: true, // 网络重新连接时重新获取

      // 错误处理
      throwOnError: false, // 不抛出错误到Error Boundary,由组件自己处理
    },
    mutations: {
      // Mutation重试策略
      retry: 0, // Mutation失败不重试 (避免重复提交)

      // 错误处理
      throwOnError: false,
    },
  },
})

/**
 * Query Keys Factory
 * 统一管理所有query keys,避免硬编码
 */
export const queryKeys = {
  // 数据源相关
  datasources: {
    all: ['datasources'] as const,
    lists: () => [...queryKeys.datasources.all, 'list'] as const,
    list: (filters: Record<string, any>) =>
      [...queryKeys.datasources.lists(), filters] as const,
    details: () => [...queryKeys.datasources.all, 'detail'] as const,
    detail: (id: number) => [...queryKeys.datasources.details(), id] as const,
  },

  // 采集任务相关
  collectionTasks: {
    all: ['collectionTasks'] as const,
    lists: () => [...queryKeys.collectionTasks.all, 'list'] as const,
    list: (filters: Record<string, any>) =>
      [...queryKeys.collectionTasks.lists(), filters] as const,
    details: () => [...queryKeys.collectionTasks.all, 'detail'] as const,
    detail: (id: number) => [...queryKeys.collectionTasks.details(), id] as const,
  },

  // 元数据相关
  metadata: {
    all: ['metadata'] as const,
    searches: () => [...queryKeys.metadata.all, 'search'] as const,
    search: (keyword: string, filters: Record<string, any>) =>
      [...queryKeys.metadata.searches(), keyword, filters] as const,
    tables: () => [...queryKeys.metadata.all, 'table'] as const,
    table: (id: number) => [...queryKeys.metadata.tables(), id] as const,
    columns: (tableId: number) => [...queryKeys.metadata.all, 'columns', tableId] as const,
  },

  // 导出任务相关
  exportTasks: {
    all: ['exportTasks'] as const,
    lists: () => [...queryKeys.exportTasks.all, 'list'] as const,
    list: (filters: Record<string, any>) => [...queryKeys.exportTasks.lists(), filters] as const,
    details: () => [...queryKeys.exportTasks.all, 'detail'] as const,
    detail: (id: number) => [...queryKeys.exportTasks.details(), id] as const,
  },
}

/**
 * 查询选项预设
 */
export const queryOptions = {
  /**
   * 列表查询选项 (带分页)
   */
  list: {
    staleTime: 2 * 60 * 1000, // 2分钟
    gcTime: 5 * 60 * 1000, // 5分钟
  },

  /**
   * 详情查询选项
   */
  detail: {
    staleTime: 5 * 60 * 1000, // 5分钟
    gcTime: 10 * 60 * 1000, // 10分钟
  },

  /**
   * 实时数据查询选项 (带轮询)
   */
  realtime: (interval: number = 3000) => ({
    staleTime: 0, // 立即过期
    refetchInterval: interval, // 定时刷新
    refetchIntervalInBackground: false, // 后台不刷新
  }),

  /**
   * 静态数据查询选项 (长时间缓存)
   */
  static: {
    staleTime: 30 * 60 * 1000, // 30分钟
    gcTime: 60 * 60 * 1000, // 1小时
  },
}
